<?php

namespace App\Logic;

use \Common\Logic\BaseLogic;

class TaskLogic extends BaseLogic
{
    private $listModel;
    private $joinModel;
    private $userModel;
    public static $listField = "id,title,cover,num,join_num,income";

    public function __construct()
    {
        parent::__construct();
        $this->listModel = D('List');
        $this->joinModel = D('Join');
        $this->userModel = D('User');
    }


    /**
     * 根据条件统计
     * @param array $where
     * @return mixed
     */
    public function statistics($where = array())
    {
        return $this->listModel->where($where)->count();
    }

    /**
     * 获取任务列表
     * @param int $page
     * @param int $size
     * @param array $where
     * @param string $userId
     * @return mixed
     */
    public function getList($page = 1, $size = 20, $where = array(), $userId = "")
    {
        $where = array_merge($where, array(
            "task_state" => array('neq', -1)
        ));
        $list = $this->listModel->field(static::$listField)->where($where)->page($page . "," . $size)->order('id desc')->select();
        $count = $this->statistics($where);

        //我参加的任务
        if (!empty($userId) && !empty($list)) {
            $taskIdArr = array();
            foreach ($list as $vo) {
                $taskIdArr[] = $vo['id'];
            }
            $joinList = $this->joinModel->where(array(
                "user_id" => $userId,
                "task_id" => array("in", $taskIdArr),
                "state" => array('neq', -1)
            ))->select();

            if (!empty($joinList)) {
                foreach ($list as &$vo) {
                    foreach ($joinList as $jvo) {
                        if ($vo['id'] == $jvo['task_id']) {
                            $vo['is_add'] = 1;
                            $vo['join_state'] = $jvo['state'];
                            break;
                        }
                    }
                }
            }
        }

        return static::$res->suc("成功", array(
            "list" => $list,
            "count" => $count
        ));
    }

    /**
     * 获取任务明细
     * @param $id
     * @return mixed
     */
    public function getDetial($id)
    {
        if (empty($id)) {
            return static::$res->err("任务参数异常");
        }

        $where = array(
            "id" => $id
        );
        $data = $this->listModel->where($where)->find();
        if (empty($data)) {
            return static::$res->err("任务不存在");
        }

        return static::$res->suc("成功", $data);
    }

    /**
     * 收入排行榜
     * @param string $type
     * @param int $size
     * @return mixed
     */
    public function ranking($type = "day", $size = 13)
    {
        switch ($type) {
            case "day":
                $startTime = date("Y-m-d") . ' 00:00:00';
                break;
            case "week":
                $startTime = date('Y-m-d H:i:s', strtotime("previous monday"));
                break;
            case "month":
                $startTime = date("Y-m-") . '01 00:00:00';
                break;
            case "all":
                $startTime = "2000-01-01 00:00:00";
                break;
            default:
                return static::$res->err("排行类型不支持");
        }


        $endTime = date('Y-m-d H:i:s');
        $where = array(
            "state" => 3,
            "finish_time" => array(
                array('EGT', $startTime),
                array('ELT', $endTime),
                'and'
            )
        );

        $rankingTable = $this->joinModel->where($where)
            ->field("user_id,sum(income) as sum_income")->
            group("user_id")->page(1, $size)->order('sum_income desc')
            ->select(false);
        $userTableName = $this->userModel->getTableName();
        $list = $this->userModel
            ->join("right join (" . $rankingTable . ") rank on rank.user_id=" . $userTableName . ".id")
            ->order('rank.sum_income desc')->select();

        return static::$res->suc("成功", $list);
    }
}
